﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Macaw.Wss3.DependencySpiderSolution;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Collections.Generic;

namespace Macaw.Wss3.DependencySpiderSolution
{
    public partial class UrlReferers : LayoutsPageBase
    {

        protected Repeater rptReferers;
        protected TextBox tbUrl;
        protected Button btnSearch;

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        public void Search_Click(object sender, EventArgs e)
        {
            ShowData(tbUrl.Text);
        }

        private void ShowData(string url)
        {            
            if (!string.IsNullOrEmpty(url))
            {
                SPSite site = SPContext.Current.Site;   
                DependencyMapStorageCollection storagecollection = new DependencyMapStorageCollection(site.WebApplication);

                DependencyMapStorage dependencymapstorage = storagecollection.GetValue<DependencyMapStorage>(site.ID);
                if (dependencymapstorage != null && !string.IsNullOrEmpty(dependencymapstorage.dependencymap))
                {
                    DependencyMap dependencymap = new DependencyMap();
                    StringReader sr = new StringReader(dependencymapstorage.dependencymap);
                    dependencymap.ReadXml(sr);
                    sr.Close();

                    DependencyMap.ResourceRow[] rows = (DependencyMap.ResourceRow[])dependencymap.Resource.Select(string.Format("Url='{0}'", url));
                    List<DependencyMap.DependencyRow> referers = new List<DependencyMap.DependencyRow>();
                    foreach (DependencyMap.ResourceRow row in rows)
                    {
                        foreach (DependencyMap.DependencyRow dependencyrow in row.GetDependencyRowsByResource_Link_Dependency())
                        {
                            referers.Add(dependencyrow);
                        }
                    }
                    rptReferers.DataSource = referers;
                    rptReferers.ItemDataBound += new RepeaterItemEventHandler(rptReferers_ItemDataBound);
                    rptReferers.DataBind();
                }
            }

        }

        void rptReferers_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DependencyMap.DependencyRow dependencyrow = (DependencyMap.DependencyRow) e.Item.DataItem;
                HtmlControl listitem = (HtmlControl ) e.Item.FindControl("listitem");
                HyperLink hyperlink = (HyperLink) e.Item.FindControl("hyperlink");
                if (dependencyrow != null && listitem != null && hyperlink!=null)
                {
                    listitem.Attributes.Add("class", "referer");
                    DependencyMap.ResourceRow refererrow = (DependencyMap.ResourceRow)dependencyrow.ResourceRowByFK_Resource_Source_Dependency;
                    hyperlink.NavigateUrl = refererrow.Url;
                    hyperlink.Text = refererrow.Url;
                }
            }
        }

        protected override void CreateChildControls()
        {
            if (!IsPostBack)
            {
                string url = HttpContext.Current.Request.QueryString["Url"];
                tbUrl.Text = url;
                ShowData(url);
            }

            base.CreateChildControls();
        }
    }
}
